// General WeChat payment call
// ===========================

;(function(global, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['wx'], factory)
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('wx'))
  } else {
    global.wxpay = factory(global.wx)
  }
})(this, function(wx) {
  var WxPay = {
    injectConfig: function(config, apis) {
      var isDebug = location.search.indexOf('debug=true') > 0
      wx.config({
        debug: isDebug,
        appId: config.appid,
        timestamp: config.timestamp,
        nonceStr: config.nonce_str,
        signature: config.signature,
        jsApiList: apis
      })
    },

    invokePay: function(config, success) {
      wx.ready(function() {
        wx.chooseWXPay({
          timestamp: config.timestamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: 'MD5',
          paySign: config.paySign,
          success: function(res) {
            success(res)
          }
        })
      })
    }
  }

  return WxPay
})